<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/WEB-INF/common/common.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="utf-8">
  <title>添加表格</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="<%=path %>/layui/css/layui.css">
  <link rel="stylesheet" href="<%=path %>/layui/css/lxq.css">
    <script type="text/javascript" src="<%=path %>/layui/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="<%=path %>/layui/layui.js" charset="utf-8"></script>
</head>
<body>
<div class="lxq-body">
	    <div class="layui-form-item">
		    <div class="layui-input-block">
				<button class="layui-btn" data-type="asTemplate">作为模板</button>
				<button class="layui-btn" data-type="asRepeat">发布复核</button>
		    </div>
		  </div>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
  <legend>修改表格</legend>
</fieldset>

<div class="layui-tab">
  <ul class="layui-tab-title">
    <li class="layui-this">基础部分</li>
    <li>内容部分</li>

  </ul>
  <div class="layui-tab-content">
    <div class="layui-tab-item layui-show">

<form class="layui-form" method="post" id="data_form">
  <div class="layui-row layui-col-space10">
		<input name="id" type="hidden" value="${form.id }"/>
		<input name="formId" type="hidden" value="${form.formId }"/>
		<input name="userName" type="hidden" value="${form.userName }"/>
		<input name="userId" type="hidden" value="${form.userId }"/>
		<input name="createTime" type="hidden" value="${form.createTime }"/>
		<input name="isDelete" type="hidden" value="${form.isDelete }"/>
		  <div class="layui-form-item">
		    <label class="layui-form-label">表格名</label>
		    <div class="layui-input-block">
		      <input type="text" name="formName" required  lay-verify="required" placeholder="请输入表格名" value="${form.formName }" autocomplete="off" class="layui-input">
		    </div>
		  </div>
		  
	
		  <div class="layui-form-item">
		    <label class="layui-form-label">表格状态</label>
		    <div class="layui-input-block">
		    		<input type="radio" name="formState" value="0" title="关闭" <c:if test="${form.formState == 0}">checked</c:if> >
		      		<input type="radio" name="formState" value="1" title="发布" <c:if test="${form.formState == 1}">checked</c:if>>
		    </div>
		  </div>
		   
		  <div class="layui-form-item">
		    <label class="layui-form-label">表格类型</label>
		    <div class="layui-input-block">
		    	
		    
		      <input type="radio" name="formType" value="0" title="普通表格" <c:if test="${form.formType == 0}">checked</c:if>>
		      <input type="radio" name="formType" value="1" title="信息复核" <c:if test="${form.formType == 1}">checked</c:if>>
		      <input type="radio" name="formType" value="2" title="重复回收" <c:if test="${form.formType == 2}">checked</c:if>>
		    </div>
		  </div>

			<div class="layui-form-item">
		      <label class="layui-form-label">开始时间</label>
		      <div class="layui-input-block">
		        <input type="text" name="startTime" id="startDate" value="${form.startTime }" placeholder="开始时间" autocomplete="off" class="layui-input">
		      </div>
		   </div>
		  
	      <div class="layui-form-item">
		      <label class="layui-form-label">截止时间</label>
		      <div class="layui-input-block">
		        <input type="text" name="endTime" id="endDate" value="${form.endTime }" placeholder="截止时间" autocomplete="off" class="layui-input">
		      </div>
		   </div>
		   
	     <div class="layui-form-item layui-form-text">
		    <label class="layui-form-label">备注</label>
		    <div class="layui-input-block">
		      <textarea name="remark" placeholder="请输入备注" class="layui-textarea">${form.remark }</textarea>
		    </div>
		  </div>
		  
	    <div class="layui-form-item">
		    <div class="layui-input-block">
		      <button class="layui-btn" lay-submit lay-filter="sb">保存修改</button>
		      
		    </div>
		  </div>
  </div>    
</form>


	`</div>
    <div class="layui-tab-item">
		<table class="layui-table" id="topicTable" lay-filter="main">
		</table>
		<button class="layui-btn" id="fieldAdd" data-type="fieldAdd">添加题目</button>
		<button class="layui-btn" data-type="getjson">保存修改</button>

	</div>
  </div>
</div>

 
 </div>
<script>
layui.use(['form', 'layedit', 'laydate','table','element'], function(){
  var form = layui.form
  ,table = layui.table
  ,layedit = layui.layedit
  ,laydate = layui.laydate;
  var formId = ${form.id};
  var index = parent.layer.getFrameIndex(window.name);
  
  table.render({
	    elem: '#topicTable'
	    ,cols: [[
	       {field:'topicName', title: '题目', align:'center', edit: 'text'}
	      ,{field:'topicType', title: '类型', align:'center', edit: 'text', templet: '#titleTpl'}
	      ,{field:'options', title: '选项', align:'center', edit: 'text'}
	      ,{fixed: 'right', title: '操作', align:'center', toolbar: '#barDemo'}
	    ]]
  		 ,data: ${json}
		 ,id: 'topicTable'
	  });
  
  
  var $ = layui.$, active = {
		    getjson: function(){
		    	var fdata = JSON.stringify(table.cache.topicTable);
		    	var formid = "${form.formId}";
			  	  $.ajax({
						type:"post",
						url:"<%=basePath%>/biz/Topic_update",
						data:{json : fdata, formId : formid},
						success: function(data){
							layer.msg("更新内容成功");
						}
				  });
		      //执行重载
		    },
		    fieldAdd: function(){
		         var oldData =  table.cache["topicTable"];
		          var data1={"topicName":"", "topicType":1};
		          oldData.push(data1);
		          table.reload('topicTable',{
		              data : oldData
		          });
		    },
		    asTemplate: function(){
		    	var url = "<%=basePath%>/biz/Form_asTemplate?id="+formId;
		    	window.location.href = url;
		    },
		    asRepeat: function(){
			  	  $.ajax({
						type:"post",
						url:"<%=basePath%>/biz/Main_asRepeat",
						data:{id:formId},
						success: function(data){
							parent.layer.close(index);
						}
				  });
		    }

		  };
		  
		  $('.layui-btn').on('click', function(){
		    var type = $(this).data('type');
		    active[type] ? active[type].call(this) : '';
		  });
		  
		  
		  //监听工具条
		  table.on('tool(main)', function(obj){
		    var data = obj.data;
			if(obj.event === 'del'){
		      layer.confirm('真的删除行么', function(index){
		    	  $.post('<%=basePath %>/biz/Topic_del',{"id":data.id},function(){});
		       	obj.del();
		        layer.close(index);
		      });
		    }
		  });
  
  
  
  
  //日期
  laydate.render({
    elem: '#startDate'
    ,type: 'datetime'
    ,format: 'yyyy-MM-dd HH:mm'
  });

  laydate.render({
    elem: '#endDate'
    ,type: 'datetime'
    ,format: 'yyyy-MM-dd HH:mm'
	});
  
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');
 
  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 5){
        return '标题至少得5个字符啊';
      }
    }
    ,pass: [/(.+){6,12}$/, '密码必须6到12位']
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });
  

  
  //监听提交
  form.on('submit(sb)', function(data){
		$.ajax({
			type:"POST",
			url:"<%=basePath%>/biz/Form_update",
			data:data.field,
			success:function(data){
				 layer.msg("更新基础信息成功");
			}
		});
    return false;
  });
 

});
</script>


<script type="text/html" id="titleTpl">
  {{#  if(d.topicType == 1){ }}
    	填空
  {{#  } }}
  {{#  if(d.topicType == 2){ }}
   		 大量文字
  {{#  } }}
  {{#  if(d.topicType == 3){ }}
    	单选
  {{#  } }}
  {{#  if(d.topicType == 4){ }}
    	多选
  {{#  } }}
</script>

    <script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script>

</body>
</html>